<?php
/**
 * 文件路径： \application\index\job\Hello.php
 * 这是一个消费者类，用于处理 helloJobQueue 队列中的任务
 */
namespace app\api\job;

use think\Db;
use think\queue\Job;
use app\common\model\BetsRecord;

class InjectionBetsRecord {

    /**
     * fire方法是消息队列默认调用的方法
     * @param Job            $job      当前的任务对象
     * @param array|mixed    $data     发布任务时自定义的数据
     */
    public function fire(Job $job,$data){
        $isJobDone = $this->updateBetsRecord($data);
        if ($isJobDone) {
            print('执行成功');
            //如果任务执行成功， 记得删除任务
            $job->delete();
        }else{
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了
                //print("<warn>Hello Job has been retried more than 3 times!"."</warn>\n");
                $job->delete();
                // 也可以重新发布这个任务
                //print("<info>Hello Job will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
        unset($data);
    }

    /**
     * 修改总投注记录
     * @param $data
     */
    public function updateBetsRecord($data)
    {
        $type = [];
        foreach ( $data as $k=> $v ){
            switch ($k){
                case 'single':
                    $type[] = 'single';
                    break;
                case 'double':
                    $type[] = 'double';
                    break;
                case  'man':
                    $type[] = 'man';
                    break;
                case 'woman':
                    $type[] = 'woman';
                    break;
                case 'close':
                    $type[] = 'close';
                    break;
                case 'far':
                    $type[] = 'far';
                    break;
                case 'magic_y':
                    $type[] = 'magic_y';
                    break;
                case 'magic_n':
                    $type[] = 'magic_n';
                    break;
                case 'yemo':
                    $type[] = 'yemo';
                    break;
                case 'tianhui':
                    $type[] = 'tianhui';
                    break;
                case 'power':
                    $type[] = 'power';
                    break;
                case 'agile':
                    $type[] = 'agile';
                    break;
                case 'intellect':
                    $type[] = 'intellect';
                    break;
                default:
                    # code...
                    break;
            }


        }
        foreach ( $type as $item ){
            $betsRecord = BetsRecord::where('game',$data['game'])->where('index',$data['sequencenum'])->where('type',$item)->find();
            if( $betsRecord ){
                $betsRecord->money = $betsRecord['money'] + $data[$item];
                $betsRecord->save();
            }else{
                $betsRecord = new BetsRecord;
                $betsRecord->data([
                    'game'  =>  $data['game'],
                    'type'  =>  $item,
                    'money' =>  $data[$item],
                    'index' =>  $data['sequencenum'],
                    //'site'          => $this->auth->site,
                    'createtime'    => time()
                ])->save();

            }
        }
        return true;
    }
}